package com.huaruan.youdi.views.activitys;


import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.huaruan.youdi.R;
import com.huaruan.youdi.eventbus.EventBus;
import com.huaruan.youdi.eventbus.EventNames;
import com.huaruan.youdi.http.okhttp.Constant;
import com.huaruan.youdi.http.okhttp.HttpCallback;
import com.huaruan.youdi.http.okhttp.HttpUtils;
import com.huaruan.youdi.utils.AppUtils;
import com.huaruan.youdi.utils.PreferenceUtil;
import com.huaruan.youdi.utils.StringUtils;

import java.util.SortedMap;
import java.util.TreeMap;

import butterknife.BindView;

public class AppealActivity extends BaseActivity {
    @BindView(R.id.ll_sale)
    LinearLayout ll_sale;
    @BindView(R.id.iv_sale)
    ImageView iv_sale;
    @BindView(R.id.ll_buy)
    LinearLayout ll_buy;
    @BindView(R.id.iv_buy)
    ImageView iv_buy;
    @BindView(R.id.ll_weather)
    LinearLayout ll_weather;
    @BindView(R.id.iv_weather)
    ImageView iv_weather;
    @BindView(R.id.ll_other)
    LinearLayout ll_other;
    @BindView(R.id.iv_other)
    ImageView iv_other;
    @BindView(R.id.et_content)
    EditText et_content;
    @BindView(R.id.tv_count)
    TextView tv_count;
    @BindView(R.id.tv_update)
    TextView tv_update;

    private String reason;
    private String waybillId;

    @Override
    public int IGetContentViewResId() {
        return R.layout.activity_appeal;
    }

    @Override
    public void IFindViews() {
        Bundle bundle = getIntent().getExtras();
        waybillId = bundle.getString("waybillId");
        ll_sale.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reason = "0";
                iv_sale.setSelected(true);
                iv_buy.setSelected(false);
                iv_weather.setSelected(false);
                iv_other.setSelected(false);
            }
        });
        ll_buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reason = "1";
                iv_sale.setSelected(false);
                iv_buy.setSelected(true);
                iv_weather.setSelected(false);
                iv_other.setSelected(false);
            }
        });
        ll_weather.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reason = "2";
                iv_sale.setSelected(false);
                iv_buy.setSelected(false);
                iv_weather.setSelected(true);
                iv_other.setSelected(false);
            }
        });
        ll_other.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reason = "3";
                iv_sale.setSelected(false);
                iv_buy.setSelected(false);
                iv_weather.setSelected(false);
                iv_other.setSelected(true);
            }
        });

        et_content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s != null) {
                    tv_count.setText(s.toString().trim().length() + "/600");
                } else {
                    tv_count.setText("0/600");
                }
                if (s.toString().trim().length() > 600) {
                    et_content.setText(s.toString().trim().substring(0, 600));
                    et_content.requestFocus();
                    et_content.setSelection(et_content.getText().length());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        tv_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addPinSingle();
            }
        });
    }

    @Override
    public void IInitData() {

    }

    private void addPinSingle() {
        if(StringUtils.isEmpty(reason)){
            AppUtils.toastShort(IGetContext(),"请先选择销单原因");
            return;
        }
        String detail = et_content.getText().toString();
        if(StringUtils.isEmpty(detail)){
            AppUtils.toastShort(IGetContext(),"请先填写具体原因");
            return;
        }

        SortedMap<String, String> mSortedMap = new TreeMap<>();
        mSortedMap.put("waybillId", waybillId);
        mSortedMap.put("tel", PreferenceUtil.getString(PreferenceUtil.PreferenceKeys.PHONE, ""));
        mSortedMap.put("destroyCause", reason);
        mSortedMap.put("detail", detail);
        new HttpUtils.Builder(IGetContext())
                .postMethod()
                .url(Constant.ADD_APPEAL)
                .params(mSortedMap)
                .callback(new HttpCallback() {
                    @Override
                    public void success(Object object) {
                        AppUtils.toastShort(IGetContext(),"申诉成功");
                        EventBus.getInstance().notifiDataUpdate(EventNames.REFRESH_MAIN, "");
                        finish();
                    }
                }).start();
    }

}
